package com.lvhr.rpc.server;

import io.vertx.core.Vertx;

/**
 * Vertx Http 服务器
 */
public class VertxHttpServer implements HttpServer{
    /**
     * 启动服务器
     * @param  port
     */
    public void doStart(int port) {
        //创建Vert.x实例
        Vertx vertx = Vertx.vertx();

        //创建HTTP服务器
        io.vertx.core.http.HttpServer server = vertx.createHttpServer();

        //监听端口并处理请求
        server.requestHandler(new HttpServerHandler());
/*        server.requestHandler(httpServerRequest -> {
            //处理http请求
            System.out.println("Received request: " + httpServerRequest.method() + " " + httpServerRequest.uri());

            //发送http响应
            httpServerRequest.response()
                    .putHeader("content-type","text/plain")
                    .end("Hello from Vert.x HTTP Server!");
        });*/

        //启动HTTP服务器并监视指定端口
        server.listen(port,httpServerAsyncResult -> {
            if (httpServerAsyncResult.succeeded()){
                System.out.println("Server is now listening on port " + port);
            }
            else {
                System.out.println("Failed to start server: " + httpServerAsyncResult.cause());
            }
        });
    }
}
